package com.qianniu.napi.admin.web.notice;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.Notice;
import com.qianniu.napi.admin.service.INoticeService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 通知
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-22
 */
@RestController
public class NoticeController {

    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/api/notice/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "type", required = false) String type,
                      @RequestParam(value = "object", required = false) String object,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        if (pageIndex == null) pageIndex = 1;
        Page page = new Page(pageIndex, pageSize);
        Wrapper<Notice> wrapper = new EntityWrapper<Notice>();
        if (StringUtils.hasText(type)){
            wrapper.eq("type", type);
        }
        if (StringUtils.hasText(object)){
            wrapper.eq("object", object);
        }
        wrapper.eq("state", 1);
//        wrapper.eq("uid", ObjectUtil.getUid());
        page = noticeService.selectPage(page, wrapper);
        info.setInfo(page.getRecords());
        return info;
    }

    /*@RequestMapping("/api/notice/add")
    @ResponseBody
    public Jinfo noticeAdd(@Valid Notice notice,
                           BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        notice.setCreatetime(new Date());
        notice.insert();
        return info;
    }

    @RequestMapping("/api/notice/modify")
    @ResponseBody
    public Jinfo noticeUpdate(HttpServletRequest req,
                              Notice notice) {
        Jinfo info = new Jinfo();
        notice.setModifytime(new Date());
        notice.updateById();
        return info;
    }

    @RequestMapping("/api/notice/detail")
    @ResponseBody
    public Jinfo getNoticeDetailById(@RequestParam("token") String token,
                                     @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Notice notice = noticeService.selectById(id);
        info.setInfo(notice);
        return info;
    }

    @RequestMapping("/api/notice/jiluFormId")
    @ResponseBody
    public Jinfo jiluFormId(@RequestParam("token") String formId) {
        Jinfo info = new Jinfo();
//        Notice notice = noticeService.selectById(id);
//        info.setInfo(notice);
        return info;
    }*/

}
